function goToBottom(){
	$(window).scrollTop($('body').height());
}

function clearCookies(id)
{
	var bookList= new Array();
	var strBookList=getCookie("booklist");
	if(strBookList!=null&&strBookList!=undefined&&strBookList.length>0){
		var arrBookList=strBookList.split(",");
		for(var i=0 ;i<arrBookList.length;i++){
			var bookItem=JSON.parse(unescape(arrBookList[i]));
			if(bookItem.BookId!=id){
				bookList[bookItem.BookId]=bookItem;
			}
		}
	}
	saveBookcase(bookList);
}

function setCookie(c_name,value)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+365)
    document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
}

function getCookie(c_name)
{
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1){
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    document.cookie= name + "=;expires="+exp.toGMTString();
}

function delBook(bookId){
   if(confirm("确认从书架中删除此书吗？"))
    {
        clearCookies(bookId);
        $("a[bookid='"+bookId+"']").parent().parent().remove();
    }
}

function delAllBook(bookId){
    delCookie("bookinfo");
    delCookie("bookid");
    delCookie("booklist");
    var chapterList= $("a[bookid]");
    if(chapterList==undefined||chapterList==null||chapterList.length==0) return;

    chapterList.each(function(){
        $(this).parent().parent().remove();
    });
}

function delBookFromBookcase(bookId){
if(confirm("确认从书架中删除此书吗？")){
    $.ajax({
		async:false,
		type:'POST',
		url:'/DoBookcase.html',
		data:"action=delete&bookid="+bookId,
		dataType:'json',
		success:function(rs) {
		    if(rs!=null&&typeof(rs.success) != 'undefined' && rs.success == 1) {
			     alert("已经成功将小说从书架删除！");
			     $("a[bookcaseid='"+bookId+"']").parent().parent().remove();

			} else {
				alert("将小说从书架删除失败！");
			}
		}
	});
}
}

function addBookMarkByJs(chapterid,bookid,chaptername){
	var currentBook={BookId:bookid,
		ChapterId:chapterid,
		ChapterName:chaptername
	};
    var bookList=getBookcase();
	if(bookList!=null&&bookList!=undefined){
		var bookcount=getBookcaseCount(bookList);
		bookList[bookid]=currentBook;
		saveBookcase(bookList);
		if(bookcount>10){
			removeBookFromTop(bookcount-10);
		}
	}
}



function addBookMarkByManual(book_id,article_id){
    if(book_id > 0 || article_id > 0){
      $.ajax({
         type: "POST",
         url: addBookMark_url,
         data: "book_id="+book_id+'&article_id='+article_id,
         success: function(msg){
            if(msg == 'success'){
              swal({
                   title:"已成功添加到我的书架",
                   text:"",
                   type:"success"
               });
            }else if(msg == 'overlv'){
                swal("您已会员等级限制","请查看会员等级说明","error");
            }else if(msg == 'over'){
                swal("您已经添加过了","","error");
            }else{
              swal({
                   title:"网络不稳定，添加失败！",
                   text:"",
                   type:"error"
               });
            }
         }
      });
    }
}

function addHistoryBookByManual2(chapterid,bookid,chaptername){
	var currentBook={BookId:bookid,
		ChapterId:chapterid,
		ChapterName:chaptername
	};
        var bookList=getBookcase();
	if(bookList!=null&&bookList!=undefined){
		var bookcount=getBookcaseCount(bookList);
		bookList[bookid]=currentBook;
		saveBookcase(bookList);
		if(bookcount>10){
			removeBookFromTop(bookcount-10);
		}
	}
}

function convertBookData(){
	var bookInfoStr=getCookie("bookinfo");
	if(bookInfoStr!=null&&bookInfoStr!=undefined){
	    	var cookiesBookInfo=bookInfoStr.split("§§");
		if(cookiesBookInfo.length>0){
    			for (var i=0;i<cookiesBookInfo.length;i++){
        			var bookInfoItem=cookiesBookInfo[i].split("||");
       				if(bookInfoItem.length==3){
					addHistoryBookByManual2(bookInfoItem[1],bookInfoItem[0],bookInfoItem[2]);
				}
    			}
		}
		delCookie("bookinfo");
	}
}

function getBookcase(){
        var bookList= new Array();
	var strBookList=getCookie("booklist");
	if(strBookList!=null&&strBookList!=undefined&&strBookList.length>0){
		var arrBookList=strBookList.split(",");
		for(var i=0;i<arrBookList.length;i++){
			var bookItem=JSON.parse(unescape(arrBookList[i]));
			bookList[bookItem.BookId]=bookItem;
		}
	}
	return bookList;
}

function removeBookFromTop(num){
    var bookList= new Array();
	var strBookList=getCookie("booklist");
	if(strBookList!=null&&strBookList!=undefined&&strBookList.length>0){
		var arrBookList=strBookList.split(",");
		for(var i=num;i<arrBookList.length;i++){
			var bookItem=JSON.parse(unescape(arrBookList[i]));
			bookList[bookItem.BookId]=bookItem;
		}
	}
	saveBookcase(bookList);
}

function getBookcaseCount(bookList){
	var count=0;
	if(bookList!=null&&bookList!=undefined&&bookList.length>0){
		for(var bookIndex in bookList){
			count++;
		}
	}
	return count;
}

function saveBookcase(bookList){
	if(bookList!=null&&bookList!=undefined&&bookList.length>0){
		var strCookieBookId="";
		var strCookieBookList="";
		for(var bookIndex in bookList){
			strCookieBookId =strCookieBookId+","+bookIndex;
			strCookieBookList=strCookieBookList+","+escape(JSON.stringify(bookList[bookIndex]));
		}
		if(strCookieBookId.length>0){
			strCookieBookId=strCookieBookId.substring(1);
		}
		if(strCookieBookList.length>0){
			strCookieBookList=strCookieBookList.substring(1);
		}
		setCookie("bookid",strCookieBookId);
		setCookie("booklist",strCookieBookList);
	}

}

